﻿using System;
using weka.core;
using weka.core.converters;
using java.io;
using weka.filters.unsupervised.attribute;
using weka.filters;

namespace OMC.Classification
{
    /// <summary>
    /// This class can manage all the parts of writing data to a file.
    /// </summary>
    public class DataWriter
    {
        private Instances rawData;
        private String fileDestination;
        private StringToWordVector filter = new StringToWordVector();

        /// <summary>
        /// set,get DataOutput format,
        /// false,if Data output is default and
        /// true, if data output is filtered
        /// </summary>
        public bool DataOutput { set; get; }

        /// <summary>
        /// The constructor to generate an object.
        /// </summary>
        /// <param name="data">Instances which contain the data</param>
        /// <param name="destination">
        /// the file Path inkl. filename of the arff data file
        /// for example: -> C:\Program Files\Microsoft Office\Office14\BAMailClassData\
        /// if you chose the folowing string as inFilePath: "./BAMailClassData/ARFF/data.arff"
        /// </param>
        public DataWriter(Instances data, String destination)
        {
            this.fileDestination = destination;
            this.rawData = data;
        }

        /// <summary>
        /// This method generate the data and write it to the ARFF File.
        /// </summary>
        public void Make()
        {
            //filtered
            if (DataOutput)
            {
                filter.setInputFormat(rawData);
                rawData = Filter.useFilter(rawData, filter);
            }
            ArffSaver saver = new ArffSaver();
            try
            {
                saver.setInstances(rawData);
                saver.setFile(new File(fileDestination));
                saver.writeBatch();
            }
            catch (IOException ex)
            {
                Utils.Debug.LogException(ex);
            }
        }
    }
}